<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>
	<div id="app">
		<input type="text" v-model="inputValue">
		<button v-on:click="btnClick">提交</button>
		<ul>
			<!-- 在向子组件传值的时候再加一个下标用于删除 -->
			<todo-item :content="item" v-for="(item,index) in list" @delete="deleteitem" :index="index"></todo-item>
		</ul>
	</div>
	<script type="text/javascript">
		var TodoItem={
			props:['content','index'],
			template:"<li @click='handleItemClick'>{{content}}</li>",
			methods:{
				handleItemClick:function(){
					// 当点击子组件的时候，子组件会向外传递一个自定义的delete事件
					this.$emit('delete',this.index)
					//把this.index作为参数传递给父组件
				}
			}
		}
		var app=new Vue({
			el:'#app',
			components:{
				TodoItem:TodoItem
			},
			data:{
				list:['1','2','3'],
				inputValue:''
			},
			methods:{
				btnClick:function(){
					this.list.push(this.inputValue);
					this.inputValue=''
				},
				deleteitem:function(index){
					// 接收到子组件的index值通过这个值从数组删除当前项
					this.list.splice(index,1)
				}
			}
		})
	</script>
</body>
</html>